package com.erikpasternak.vader;

import android.content.Context;
import android.widget.Toast;

public class DrinkPourHelper {

    private int mCurrentIngredient = 0;
    private byte[][] mIngredientMessages;

    private float mCost = 0;
    private String mName;
    private String mComments;

    private VaderActivity.VaderStateListener mListener;
    private AdkHelper mAdkHelper;

    private Context mContext;

    private final AdkHelper.Listener mAdkListener = new AdkHelper.Listener() {

        public void onSendFailed(int command) {
            if (command == AdkHelper.COMMAND_MIX) {
                Toast.makeText(mContext, "Error sending, try again.", Toast.LENGTH_SHORT)
                        .show();
                mAdkHelper.removeListener(mAdkListener);
                mListener.onStateChange(VaderActivity.STATE_DRINK);
            }
        }

        public void onMessageReceived(int command, int length, byte[] data) {
            if (command == AdkHelper.ADK_COMMAND_INGREDIENT_DONE) {
                if (mIngredientMessages == null) {
                    return;
                }
                if (mCurrentIngredient == mIngredientMessages.length) {
                    mAdkHelper.sendCommand(AdkHelper.COMMAND_MIX,
                            (byte) AdkHelper.RETURN_MSG_VALUES.length, AdkHelper.RETURN_MSG_VALUES);
                } else if (mCurrentIngredient < mIngredientMessages.length) {
                    byte[] values = mIngredientMessages[mCurrentIngredient];
                    mAdkHelper.sendCommand(AdkHelper.COMMAND_MIX, (byte) values.length, values);
                } else {
                    mAdkHelper.removeListener(mAdkListener);
                    mListener.onStateChange(VaderActivity.STATE_INITIAL);
                }
                mCurrentIngredient++;
            }
        }
    };

    public DrinkPourHelper(VaderActivity va) {
        mAdkHelper = va.getAdkHelper();
        mListener = va.getListener();
        mContext = va;
    }

    public void setIngredientMessages(byte[][] messages) {
        mIngredientMessages = messages;
        mCurrentIngredient = 0;
    }

    public void setCost(float cost) {
        mCost = cost;
    }

    public float getCost() {
        return mCost;
    }

    public void setName(String name) {
        mName = name;
    }

    public String getName() {
        return mName;
    }

    public void setComments(String comments) {
        mComments = comments;
    }

    public String getComments() {
        return mComments;
    }

    public void sendDrink() {
        mAdkHelper.addListener(mAdkListener);
        byte[] values = mIngredientMessages[0];
        mCurrentIngredient = 1;
        mAdkHelper.sendCommand(AdkHelper.COMMAND_MIX, (byte) values.length, values);
    }

    public void cancel() {
        mIngredientMessages = null;
        mAdkHelper.sendCommand(AdkHelper.COMMAND_MIX, (byte) AdkHelper.RETURN_MSG_VALUES.length,
                AdkHelper.RETURN_MSG_VALUES);
    }
}
